package com.gpframework.module.controller.purchase;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.purchase.domain.GpIngredientsTable;
import com.gpframework.module.purchase.domain.req.GpIngredientsTableAdd;
import com.gpframework.module.purchase.domain.req.GpIngredientsTableReq;
import com.gpframework.module.purchase.domain.req.GpIngredientsTableUpdate;
import com.gpframework.module.purchase.domain.vo.GpIngredientsTableVo;
import com.gpframework.module.purchase.service.IGpIngredientsTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName GpIngredientsTableController
 * @Author gpframework
 * @Date 2023-12-22
 * @Version 1.0.0
 * @Description 欧根纱成分Controller
 */
@Validated
@Api(tags = "欧根纱成分")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/purchase/ingredientsTable" )
public class GpIngredientsTableController extends BaseController {

    private final IGpIngredientsTableService iGpIngredientsTableService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-22
  * @Param gpIngredientsTable
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('purchase:ingredientsTable:list')")
    @GetMapping("/list")
    public TableDataInfo<GpIngredientsTableVo> list(GpIngredientsTableReq gpIngredientsTableReq)
    {
        startPage();
        QueryWrapper<GpIngredientsTable> lqw = new QueryWrapper<GpIngredientsTable>();
        gpIngredientsTableReq.generatorQuery(lqw,true);
        List<GpIngredientsTableVo> list = iGpIngredientsTableService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpIngredientsTable
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('purchase:ingredientsTable:add')" )
    @Log(title = "添加欧根纱成分" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpIngredientsTableAdd gpIngredientsTableAdd) {
        GpIngredientsTable gpIngredientsTable = Convert.convert(new TypeReference<GpIngredientsTable>() {}, gpIngredientsTableAdd);
        return toAjax(iGpIngredientsTableService.save(gpIngredientsTable) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpIngredientsTable
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('purchase:ingredientsTable:edit')" )
    @Log(title = "修改欧根纱成分" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpIngredientsTableUpdate gpIngredientsTableUpdate) {
        GpIngredientsTable gpIngredientsTable = Convert.convert(new TypeReference<GpIngredientsTable>() {}, gpIngredientsTableUpdate);
        return toAjax(iGpIngredientsTableService.updateById(gpIngredientsTable) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpIngredientsTable
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('purchase:ingredientsTable:export')" )
    @Log(title = "导出欧根纱成分" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpIngredientsTableReq gpIngredientsTableReq) {
        QueryWrapper<GpIngredientsTable> lqw = new QueryWrapper<GpIngredientsTable>();
        gpIngredientsTableReq.generatorQuery(lqw,true);
        List<GpIngredientsTableVo> list = iGpIngredientsTableService.listVo(lqw);
        ExcelUtil<GpIngredientsTableVo> util = new ExcelUtil<GpIngredientsTableVo>(GpIngredientsTableVo. class);
        return util.exportExcel(list, "ingredientsTable" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpIngredientsTable
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('purchase:ingredientsTable:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpIngredientsTableVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpIngredientsTableService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpIngredientsTable
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('purchase:ingredientsTable:remove')" )
    @Log(title = "删除欧根纱成分" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpIngredientsTableService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("下载模板")
    @PreAuthorize("@ss.hasPermi('purchase:ingredientsTable:export')" )
    @Log(title = "下载模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult importTemplate() {
        ExcelUtil<GpIngredientsTableVo> util = new ExcelUtil<GpIngredientsTableVo>(GpIngredientsTableVo.class);
        return util.importTemplateExcel("欧根纱成分基础表");
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("导入")
    @PreAuthorize("@ss.hasPermi('purchase:ingredientsTable:add')" )
    @Log(title = "导入" , businessType = BusinessType.INSERT)
    @PostMapping("/import")
    public AjaxResult importList(MultipartFile file) throws Exception {
        if (file == null) {
            return AjaxResult.error("获取不到导入文件");
        }
        boolean excel = ExcelUtil.isExcel(file.getOriginalFilename());
        if (!excel) {
            return AjaxResult.error("导入文件格式错误，请导入excel文件");
        }
        // 删除旧数据
        iGpIngredientsTableService.remove(new QueryWrapper<GpIngredientsTable>().eq("del_status", YesNoEnum.NO.getKey()));
        // 导入新数据
        ExcelUtil<GpIngredientsTableVo> util = new ExcelUtil<>(GpIngredientsTableVo.class);
        List<GpIngredientsTableVo> excelList = util.importCacheExcel(file.getInputStream());
        List<GpIngredientsTable> list = Convert.convert(new TypeReference<List<GpIngredientsTable>>() {}, excelList);
        return iGpIngredientsTableService.saveBatch(list) ? AjaxResult.success("导入成功") : AjaxResult.error();
    }
}
